package com.bishe.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.ClaTeacher;
import com.bishe.entity.ScClass;
import com.bishe.entity.User;
import com.bishe.entity.resp.*;
import com.bishe.exception.CommonException;
import com.bishe.mapper.UserMapper;
import com.bishe.service.ScClassService;
import com.bishe.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wwz
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-04-03 23:14:54
 */
@AllArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    private final ScClassService classService;


    @Override
    public Page<User> page(PageParam pageParam) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isAllNotEmpty(pageParam.getSearchKey(), pageParam.getSearchValue())) {
            queryWrapper.eq(true, StrUtil.toUnderlineCase(pageParam.getSearchKey()), pageParam.getSearchValue());
        }

        if (ObjectUtil.isAllNotEmpty(pageParam.getSortField(), pageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(pageParam.getSortOrder());
            queryWrapper.orderBy(true, pageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(pageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(User::getId);
        }

        Page<User> page = this.page(CommonPageRequest.defaultPage(), queryWrapper);
        for (User record : page.getRecords()) {
            ScClass byId = classService.getById(record.getStuClass());
            if (byId != null) {
                record.setStuClassName(byId.getGrade() + "-" + byId.getClassName());
            }
        }
        return page;
    }

    @Override
    public UserInfo login(LoginInfo loginInfo) {
        // 检查登录信息
        if (loginInfo == null) {
            throw new CommonException("登录失败!");
        }

        if (StrUtil.isEmpty(loginInfo.getUserName()) || StrUtil.isEmpty(loginInfo.getPassword())) {
            throw new CommonException("用户名密码为空!");
        }

        String userName = loginInfo.getUserName();
        String password = loginInfo.getPassword();

        // 登录用户查询
        User one = getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, userName)
                .eq(User::getPassword, password), false);
        if (one == null) {
            return null;
        }

        StpUtil.login(one.getId());

        UserInfo userInfo = new UserInfo();
        userInfo.setId(one.getId());
        userInfo.setName(one.getUsername());
        userInfo.setType(one.getType());
        userInfo.setToken(StpUtil.getTokenValue());
        userInfo.setAvatar(one.getAvatar());
        return userInfo;
    }

    @Override
    public List<User> myClass(String id) {
        String stuClass = getById(id).getStuClass();
        List<User> userList = list().stream().filter(user -> stuClass.equals(user.getStuClass()) && user.getType().equals("s")).collect(Collectors.toList());
        for (User user : userList) {
            ScClass byId = classService.getById(user.getStuClass());
            if (byId != null) {
                user.setStuClassName(byId.getGrade() + "-" + byId.getClassName());
            }
        }
        return userList;
    }
}




